<?php

namespace app\shop\controller\coupon;

use think\Controller;

class Receive extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->BusinessModel = model('Business.Business');
        $this->CouponModel = model('Coupon.Coupon');
        $this->ReceiveModel = model('Coupon.Receive');

        $this->business = [];
        // 查询用户是否存在
        if($this->request->isPost())
        {
            $busid=$this->request->param('busid',0,'trim');
            $this->business=$this->BusinessModel->find($busid);
            if(!$this->business)
            {
                $this->error('用户不存在');
                exit;
            }
        }
    }
    
    public function index()
    {
        if($this->request->isPost())
        {
            $list=$this->ReceiveModel
                ->alias('receive')
                ->with(['coupon'])
                ->where(['busid'=>$this->business->id, 'receive.status' => '0'])
                ->select();
            if($list)
            {
                $this->success('优惠劵列表',null,$list);
                exit;
            }else
            {
                $this->error('暂无优惠劵');
                exit;
            }
        }
    }
    public function add()
    {
        if($this->request->isPost())
        {
            $cid=$this->request->param('cid',0,'trim');
            $coupon=$this->CouponModel->find($cid);
            if(!$coupon)
            {
                $this->error('优惠劵不存在');
                exit;
            }

            $where=[
                'cid'=>$cid,
                'busid'=>$this->business->id
            ];
            $receive=$this->ReceiveModel->where($where)->find();
            if($receive)
            {
                $this->error('您已领取过，不能重复领取');
                exit;
            }

            $count = $this->ReceiveModel->where(['cid'=>$cid])->count();
            if($count >= $coupon['nums'])
            {
                $this->error("领取人数已达到上限，无法领取");
                exit;
            }

            // 组装数据
            $data=[
                'cid'=>$cid,
                'busid'=>$this->business->id,
                'status'=>'0'
            ];
            $result=$this->ReceiveModel->validate('common/Coupon/Receive')->save($data);
            if($result === FALSE)
            {
                $this->error($this->ReceiveModel->getError());
                exit;
            }else
            {
                $this->success('领取成功');
                exit;
            }
        }
    }

    public function list()
    {
        if($this->request->isPost())
        {
            $status=$this->request->param('status','all','trim');
            $where=[
                'busid'=>$this->business->id,
            ];
            if($status != 'all')
            {
                $where['receive.status']=$status;
            }
            // halt($status);
            $list=$this->ReceiveModel->with(['coupon'])->where($where)->select();
            if($list)
            {
                $this->success('返回领取到的优惠劵',null,$list);
                exit;
            }else
            {
                $this->error('暂无更多优惠劵');
                exit;
            }

        }
    }

    // public function time()
    // {
    //         echo time() + 24*3600*10;
    //         echo "<br/>";
    //         echo time() + 24*3600*20;
    //         exit;
    // }
}